package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 群组成员设置VO - 遵循单一职责原则
 * 只负责群组成员设置信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "群组成员设置VO")
public class ChatGroupMemberSettingVO {

    @Schema(description = "设置ID")
    private String id;

    @Schema(description = "群组ID")
    private String groupId;

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "群内昵称")
    private String nickname;

    @Schema(description = "是否置顶")
    private Boolean isTop;

    @Schema(description = "是否免打扰")
    private Boolean isMuted;

    @Schema(description = "消息提醒设置")
    private Integer messageNotification;

    @Schema(description = "是否显示成员昵称")
    private Boolean showMemberNickname;

    @Schema(description = "是否保存到通讯录")
    private Boolean saveToContacts;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "群组名称")
    private String groupName;

    @Schema(description = "群组头像")
    private String groupAvatar;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "用户头像")
    private String userAvatar;

    @Schema(description = "用户真实姓名")
    private String realName;

    /**
     * 获取显示的昵称
     * 
     * @return 显示昵称
     */
    public String getDisplayNickname() {
        if (nickname != null && !nickname.trim().isEmpty()) {
            return nickname;
        }
        if (realName != null && !realName.trim().isEmpty()) {
            return realName;
        }
        return username;
    }

    /**
     * 检查是否置顶
     * 
     * @return 是否置顶
     */
    public boolean isTop() {
        return isTop != null && isTop;
    }

    /**
     * 检查是否免打扰
     * 
     * @return 是否免打扰
     */
    public boolean isMuted() {
        return isMuted != null && isMuted;
    }

    /**
     * 检查是否显示成员昵称
     * 
     * @return 是否显示成员昵称
     */
    public boolean showMemberNickname() {
        return showMemberNickname != null && showMemberNickname;
    }

    /**
     * 检查是否保存到通讯录
     * 
     * @return 是否保存到通讯录
     */
    public boolean saveToContacts() {
        return saveToContacts != null && saveToContacts;
    }

    /**
     * 获取消息提醒设置名称
     * 
     * @return 消息提醒设置名称
     */
    public String getMessageNotificationName() {
        if (messageNotification == null) {
            return "默认";
        }
        switch (messageNotification) {
            case 0:
                return "不接收消息";
            case 1:
                return "接收所有消息";
            case 2:
                return "仅接收@我的消息";
            case 3:
                return "仅接收重要消息";
            default:
                return "未知";
        }
    }

    /**
     * 获取置顶状态名称
     * 
     * @return 置顶状态名称
     */
    public String getTopStatusName() {
        return isTop() ? "已置顶" : "未置顶";
    }

    /**
     * 获取免打扰状态名称
     * 
     * @return 免打扰状态名称
     */
    public String getMuteStatusName() {
        return isMuted() ? "已免打扰" : "正常";
    }

    /**
     * 获取显示昵称设置名称
     * 
     * @return 显示昵称设置名称
     */
    public String getShowNicknameStatusName() {
        return showMemberNickname() ? "显示昵称" : "显示用户名";
    }

    /**
     * 获取通讯录设置名称
     * 
     * @return 通讯录设置名称
     */
    public String getContactsStatusName() {
        return saveToContacts() ? "已保存到通讯录" : "未保存到通讯录";
    }

    /**
     * 获取设置状态标识
     * 
     * @return 状态标识
     */
    public String getSettingStatusBadge() {
        if (isTop()) {
            return "top";
        } else if (isMuted()) {
            return "muted";
        } else {
            return "normal";
        }
    }

    /**
     * 获取设置摘要
     * 
     * @return 设置摘要
     */
    public String getSettingSummary() {
        StringBuilder summary = new StringBuilder();
        
        if (isTop()) {
            summary.append("置顶 ");
        }
        if (isMuted()) {
            summary.append("免打扰 ");
        }
        if (showMemberNickname()) {
            summary.append("显示昵称 ");
        }
        if (saveToContacts()) {
            summary.append("保存到通讯录 ");
        }
        
        summary.append(getMessageNotificationName());
        
        return summary.toString().trim();
    }

    /**
     * 获取显示的群组名称
     * 
     * @return 显示群组名称
     */
    public String getDisplayGroupName() {
        if (groupName != null && !groupName.trim().isEmpty()) {
            return groupName;
        }
        return "未命名群组";
    }

    /**
     * 获取显示的群组头像
     * 
     * @return 显示群组头像
     */
    public String getDisplayGroupAvatar() {
        if (groupAvatar != null && !groupAvatar.trim().isEmpty()) {
            return groupAvatar;
        }
        return "/images/default-group-avatar.png";
    }

    /**
     * 获取显示的用户头像
     * 
     * @return 显示用户头像
     */
    public String getDisplayUserAvatar() {
        if (userAvatar != null && !userAvatar.trim().isEmpty()) {
            return userAvatar;
        }
        return "/images/default-user-avatar.png";
    }

    /**
     * 检查是否有自定义昵称
     * 
     * @return 是否有自定义昵称
     */
    public boolean hasCustomNickname() {
        return nickname != null && !nickname.trim().isEmpty();
    }

    /**
     * 获取设置优先级
     * 
     * @return 设置优先级（数字越大优先级越高）
     */
    public int getSettingPriority() {
        int priority = 0;
        if (isTop()) {
            priority += 100;
        }
        if (isMuted()) {
            priority += 10;
        }
        if (hasCustomNickname()) {
            priority += 1;
        }
        return priority;
    }

    /**
     * 检查设置是否为默认值
     * 
     * @return 是否为默认设置
     */
    public boolean isDefaultSetting() {
        return !isTop() && !isMuted() && !hasCustomNickname() && 
               showMemberNickname() && !saveToContacts() && 
               (messageNotification == null || messageNotification == 1);
    }

    /**
     * 获取设置变更提示
     * 
     * @return 设置变更提示
     */
    public String getSettingChangeHint() {
        if (isDefaultSetting()) {
            return "使用默认设置";
        }
        
        StringBuilder hint = new StringBuilder("已自定义: ");
        boolean hasCustom = false;
        
        if (isTop()) {
            hint.append("置顶");
            hasCustom = true;
        }
        if (isMuted()) {
            if (hasCustom) hint.append("、");
            hint.append("免打扰");
            hasCustom = true;
        }
        if (hasCustomNickname()) {
            if (hasCustom) hint.append("、");
            hint.append("昵称");
            hasCustom = true;
        }
        if (!showMemberNickname()) {
            if (hasCustom) hint.append("、");
            hint.append("显示设置");
            hasCustom = true;
        }
        if (saveToContacts()) {
            if (hasCustom) hint.append("、");
            hint.append("通讯录");
            hasCustom = true;
        }
        if (messageNotification != null && messageNotification != 1) {
            if (hasCustom) hint.append("、");
            hint.append("消息提醒");
        }
        
        return hint.toString();
    }
}
